#include "HiKernel/HiFramework.h"
#include "HiKernel/HiGBRenderer.h"
#include "HiModules/mHiDummy/HiDummy.h"
#include "HiModules/mHiDummy/LM_Anim.h"

#include <NiApplication.h>



mHiDummy::mHiDummy(HiXmlNode*xml)
{
	NiInit();
}

enum State_Active
{
	MSG_ACTIVE,
	MSG_NONACTIVE

};

bool mHiDummy::HandleMessage(const Telegram& msg)
{
	HiModule *pSender = ModuleManager->FindModule(msg.Sender);

	std::cout << pSender->GetName().c_str() << " --- " <<  msg.Msg << std::endl;

	switch ( msg.Msg )
	{
	case MSG_ACTIVE: 
		this->m_bActive = true;
		break;
	}

	return true;
}



void
mHiDummy::PostConfig()
{

	
	HiGBRenderer* gbRender = (HiGBRenderer*)HiViewer::GetInstance()->GetRenderer();

	if ( gbRender->GetRenderType() != HiRenderer::Gamebryo )
		exit(-1);

}

void mHiDummy::Update()
{
	HiGBRenderer* gbRender = (HiGBRenderer*)HiViewer::GetInstance()->GetRenderer();

	if ( gbRender->GetRenderType() != HiRenderer::Gamebryo )
		exit(-1);


	NiInputGamePad* pkGamePad = NULL; 
	NiInputKeyboard* pkKeyboard = NULL;


	if(gbRender->m_spInputSystem )
	{
		if (gbRender->m_spInputSystem->GetMode() == NiInputSystem::MAPPED)
		{
			if (gbRender->m_spInputSystem->UpdateActionMap() == NIIERR_DEVICELOST)
				gbRender->m_spInputSystem->HandleDeviceChanges();
		}
		else
		{
			if (gbRender->m_spInputSystem->UpdateAllDevices() == NIIERR_DEVICELOST)
				gbRender->m_spInputSystem->HandleDeviceChanges();
		}
	}

	if (gbRender->GetInputSystem())
	{
		pkKeyboard = gbRender->GetInputSystem()->GetKeyboard();
	}
	if (pkKeyboard != NULL)
	{
		if (pkKeyboard->KeyIsDown(NiInputKeyboard::KEY_ESCAPE))
		{
			Dispatch->DispatchMessage(0, this->GetName(), "gui", MSG_ACTIVE, (int*)1);
			Dispatch->DispatchMessage(0, this->GetName(), "hud", MSG_NONACTIVE, 0);
			Dispatch->DispatchMessage(0, this->GetName(), "terrain", MSG_NONACTIVE, 0);
			Dispatch->DispatchMessage(0, this->GetName(), "character", MSG_NONACTIVE, 0);
			Dispatch->DispatchMessage(0, this->GetName(), "gbcamera", MSG_NONACTIVE, 0);


			this->m_bActive = false;
		}
	}
}

void mHiDummy::Render()
{

}

extern "C" HI_DLLEXPORT
mHiDummy* HiLoadModule_mHiDummy(HiXmlNode* xml)
{
	mHiDummy *m= new mHiDummy(xml);
	return m;
}
